package com.base.cn.platform.os.service.classes.topic.type;


import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.dao.classes.topic.type.ClassesTopicTypeDao;
import com.base.cn.platform.os.entity.classes.topic.type.ClassesTopicType;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 班级话题分类
 *
 * @author zjh
 * @since 2018-05-17
 */
@Service
public class ClassesTopicTypeBiz extends BaseBiz<ClassesTopicType, ClassesTopicTypeDao> {


    @Autowired
    private ClassesTopicTypeDao classesTopicTypeDao;

    /**
     * 分页查询话题分类列表
     * @param type
     * @return
     */
    public PageInfo<ClassesTopicType> findClassesTopicTypePage(ClassesTopicType type, Pagination pagination){
        //获取sql
        String sql = this.whereSql(type);
        PageInfo<ClassesTopicType> classesTopicTypePageInfo = this.findPage(sql,pagination,null);
        return classesTopicTypePageInfo;
    }

    /**
     * 分页查询话题分类
     * @classId 班级id
     * @return
     */
    public List<Map<String,Object>> getClassesTopicType(BigDecimal classesId){

        return classesTopicTypeDao.getClassesTopicTypeList(ClassesTopicType.class,classesId);
    }


    /**
     * 更新问答数量
     * @param typeId 类型Id
     * @param num 数量
     * @return
     */
    @Async
    public void updateClassesTopicTypeNum(BigDecimal  typeId,int num){
        ArrayList<String> columnName = new ArrayList<>();
        columnName.add("id");
        columnName.add("num");
        ClassesTopicType byId = this.findById(typeId,columnName);
        byId.setNum(byId.getNum()+num);
        this.updateById(byId,null);
    }


    /**
     * 更新话题类型状态
     * @classId 班级id
     * @return
     */
    public void updateTopicTypeStatus(String ids,int status){
        ArrayList<ClassesTopicType> objects = new ArrayList<>();
        String[] split = ids.split(",");
        for(String id:split){
            ClassesTopicType classesTopicType = new ClassesTopicType();
            classesTopicType.setId(new BigDecimal(id));
            classesTopicType.setStatus(status);
            objects.add(classesTopicType);
        }
        this.updateBatch(objects);
    }

    /**
     * 更新话题类型状态
     * @classId 班级id
     * @return
     */
    public List<Map<String,Object>> getClassesTypeNum(BigDecimal classId,BigDecimal userId){
        Map<String,Object> parameter  = new HashMap<>();
        parameter.put("classId",classId);
        parameter.put("userId",userId);
        return classesTopicTypeDao.getClassesTypeNum(ClassesTopicType.class,parameter);
    }


    private  String whereSql(ClassesTopicType  type){
        StringBuffer whereSql = new StringBuffer(" status !=3 ");
        if (StringUtils.isNotEmpty(type.getName())) {
            whereSql .append(" and name like '%").append(type.getName().trim()).append("%'");

        }
        if(DataUtil.idIsNotNull(type.getClassId())){
            whereSql.append("and  classId = ").append(type.getClassId());
        }


        return  whereSql.toString();

    }

}
